package cn.itsource.pethome.system.user.contrllor;

import cn.itsource.pethome.system.user.domain.Employee;
import cn.itsource.pethome.system.user.query.EmployeeQuery;
import cn.itsource.pethome.system.user.service.IEmployeeService;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeContrllor {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    @ApiOperation(value = "查询员工所有数据" )
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id查询员工所有数据" )
    public Employee findById(@PathVariable  Long id){
        return employeeService.findnoe(id);
    }

    @PostMapping("pagelist")
    @ApiOperation(value = "分页查询员工所有数据" )
    public AjaxResult pageData(@RequestBody EmployeeQuery query){
        try {
            List<Employee> employees = employeeService.queryData(query);//获取到所有数据
            Long count = employeeService.queryCount(query);//获取到所有数据
            return AjaxResult.success().setSuccess(true).setData(employees).setTotal(count);//将获取的数据和总条数返回给前端
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除员工数据")
    public AjaxResult delete(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @PutMapping
    @ApiOperation(value = "根据id修改员工数据，没有id就添加数据")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            }else {
                employeeService.add(employee);
            }
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @PatchMapping
    @ApiOperation(value = "批量删除员工数据")
    public AjaxResult bacthDelete(@RequestBody  List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }
}
